package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	// 文件拷贝
	srcPath := "d:/1.png"
	targetPath := "e:/1.png"
	_, err := CopyFile(srcPath, targetPath)
	if err != nil {
		fmt.Println("CopyFile error", err)
	} else {
		fmt.Println("CopyFile success")
	}
}

//文件拷贝
func CopyFile(src string, target string) (written int64, err error) {
	srcFile, err := os.Open(src)
	if err != nil {
		fmt.Println("Open file error", err)
		return
	}
	defer srcFile.Close()
	// 获取 reader
	reader := bufio.NewReader(srcFile)
	// 打开 targetFlie
	targetFlie, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE, 06666)
	if err != nil {
		fmt.Println("open file error", err)
		return
	}
	defer targetFlie.Close()
	// 获取 writer
	writer := bufio.NewWriter(targetFlie)

	return io.Copy(writer, reader)

}
